from dataclasses import dataclass

from datetime import datetime
import json

import pandas as pd


@dataclass
class commentInfo:
    mid: int
    昵称: str
    评论内容: str
    评论时间: str
    点赞数: int
    性别: str
    等级: str
    评论时间戳: int
    账号认证类型: str
    oid: int
    rpid: int
    root: int
    原评论内容: str

    def to_dict(self):
        return {
            'mid': self.mid,
            '昵称': self.昵称,
            '评论内容': self.评论内容,
            '评论时间': self.评论时间,
            '点赞数': self.点赞数,
            '性别': self.性别,
            '等级': self.等级,
            '评论时间戳': self.评论时间戳,
            '账号认证类型': self.账号认证类型,
            'oid': self.oid,
            'rpid': self.rpid,
            'root':self.root,
            '原评论内容': self.原评论内容
        }


def parse_info(filePath):
    with open(filePath, 'r', encoding='utf-8') as f:
        replies = json.load(f)
    commentInfo_list=[]
    for reply in replies:
        ctime = reply['ctime']
        official_verify_type = reply['member']['official_verify']['type']
        root = reply['root']
        originReplyContent =''
        if root !=0:
            originReplyList = list(filter(lambda x: x['rpid'] == root, replies))
            if originReplyList:
                originReplyContent =originReplyList[0]['content']['message']
        comment = commentInfo(
            mid=reply['mid'],
            昵称=reply['member']['uname'],
            评论内容=repr(reply['content']['message']),
            评论时间=datetime.fromtimestamp(ctime).strftime('%Y-%m-%d %H:%M:%S'),
            点赞数=reply['like'],
            性别=reply['member']['sex'],
            等级=reply['member']['level_info']['current_level'],
            评论时间戳=ctime,
            账号认证类型='无认证' if official_verify_type == -1 else '个人认证' if official_verify_type == 0 else '企业认证',
            oid=reply['oid'],
            rpid=reply['rpid'],
            root = root,
            原评论内容=repr(originReplyContent)
        )
        commentInfo_list.append(comment.to_dict())
        
    df = pd.DataFrame(commentInfo_list)
    df.to_csv('replies.csv', index=False, encoding='utf-8', sep='\t')


if __name__ == '__main__':
    file = '../av1750499714评论响应.json'
    parse_info(file)